import { bg_image } from "./indoorConfig.js";
import {getUserInfo,} from "../api/home/index.js"
import {sendsms,bindPhoneNumber} from "../api/personal/index.js"
export default {
	data() {
		return {
			tips: '获取验证码',//验证吗显示文字
			seconds: 60,//验证码倒计时
			bg_image,
			form: {
				phoneNumber: '',
				code: null
			},
			showError: false,
			phoneNumber: '',
			redirect: 0 ,//判断是否要重定向到月卡购买页
			options:{},//页面参数
		}
	},
	onLoad(options) {
		this.options = options
		
	},
	onReady() {
		this.fetchSomething()
		let options = this.options
		if (options.redirect) {
			this.redirect = options.redirect
		}
	},
	methods: {
		async fetchSomething(query){
			this.getUser()
		},
		codeChange(text) {//验证吗文字显示
			this.tips = text;
		},
		async getUser() {
			let res = await getUserInfo()
			let data = res.data || {}
			this.phoneNumber = data.phoneNumber
			uni.setStorageSync('userInfo',data)
		},
		go(url, item) {
			uni.navigateTo({
				url: `${url}`
			})
		},
		async getCode() {
			// if (!/^1[3456789]\d{9}$/.test(this.form.phoneNumber)) {
			// 	this.showError = true;
			// 	return false;
			// } else {
			// 	this.showError = false
			// 	let res = sendsms(this.form.phoneNumber)
			// }
			this.showError = false
			if(!this.$u.test.mobile(this.form.phoneNumber)) {
				return this.$refs.uToast.show({
						title: '请输入正确手机号~',
						type: 'warning',
					})
			}
			console.log(this.$refs.uCode)
			if(this.$refs.uCode.canGetCode) {
				if(this.phoneNumber === this.form.phoneNumber) {
					this.$u.toast('手机号已被绑定');
				}
				// 模拟向后端请求验证码
				uni.showLoading({
					title: '正在获取验证码'
				})
				// let res = await this.$u.api.verifySmsCode({
				// 	phone:this.form.phone,
				// })
				let res = await sendsms(this.form.phoneNumber)
				uni.hideLoading();
				// 通知验证码组件内部开始倒计时
				this.$refs.uCode.start();
				if(!res) return
				// 这里此提示会被this.start()方法中的提示覆盖
				this.$u.toast('验证码已发送');
				
			} else {//倒计时结束后再发送
			}
		},
		handleSure() {
			let res = bindPhoneNumber({
				phoneNumber: this.form.phoneNumber,
				code: this.form.code,
			})
			if(!res) return
			uni.showModal({
				title: '提示',
				content: '绑定手机号成功',
				showCancel: false,
				success: (res)=> {
					if (res.confirm) {
						uni.navigateBack({
							delta: 1
						});
					}
				}
			});
		},
	},

}